<?php
$host = 'localhost';
$username = '';
$password = '';
$dbname = '';
$id = $_POST['id'];
$num = $_POST['num'];
$mysql = new mysqli($host, $username, $password, $dbname);
if ($mysql->connect_errno) {
    die('数据库连接失败：' . $mysql->connect_errno);
} else {
    $sql1 = "SELECT * from bookinfo where book_id=$id";
    $result1 = $mysql->query($sql1);
    $row1 = mysqli_fetch_row($result1);
    if ($row1[5] == 0) {
        $sql2 = "UPDATE bookinfo SET num=$num,on_shelf='是' where book_id=$id";
        $result2 = $mysql->query($sql2);
        $sql4 = "INSERT INTO add_log(book_id,book_name,author,ver,num) values('$row1[0]','$row1[1]','$row1[2]','$row1[3]',$num)";
        $result4 = $mysql->query($sql4);
        if ($result2) {
            if ($result4) {
                echo "<script>alert('操作完成')</script>";
            }
        } else {
            echo "<script>alert('操作失败')</script>";
        }
    } else {
        $sql3 = "UPDATE bookinfo SET num=num+$num where book_id=$id";
        $result3 = $mysql->query($sql3);
        $sql5 = "INSERT INTO add_log(book_id,book_name,author,ver,num) values('$row1[0]','$row1[1]','$row1[2]','$row1[3]',$num)";
        $result5 = $mysql->query($sql5);
        if ($result3) {
            if ($result5) {
                echo "<script>alert('操作完成')</script>";
            }
        } else {
            echo "<script>alert('操作失败')</script>";
        }
    }
    $mysql->close();
}
?>
<a href="delete.html">继续</a> 或者 <a href="homepage.html">回到首页</a>